// 管理员登录JavaScript

document.addEventListener('DOMContentLoaded', function() {
    console.log('管理员登录页面已加载');
    
    const loginForm = document.getElementById('adminLoginForm');
    const loginBtn = document.getElementById('loginBtn');
    
    loginForm.addEventListener('submit', async function(e) {
        e.preventDefault();
        
        const password = document.getElementById('adminPassword').value;
        
        // 清除之前的错误
        document.getElementById('passwordError').textContent = '';
        document.getElementById('adminPassword').classList.remove('error');
        
        // 验证输入
        if (!password) {
            document.getElementById('passwordError').textContent = '请输入管理员密码';
            document.getElementById('adminPassword').classList.add('error');
            return;
        }
        
        // 显示加载状态
        loginBtn.disabled = true;
        loginBtn.querySelector('span:first-child').style.display = 'none';
        loginBtn.querySelector('.btn-loading').style.display = 'inline';
        
        try {
            // 发送登录请求
            const response = await fetch('/api/admin/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ password })
            });
            
            const data = await response.json();
            
            if (data.success) {
                // 登录成功
                showSuccess('登录成功！正在跳转...');
                setTimeout(() => {
                    window.location.href = '/admin';
                }, 1000);
            } else {
                // 登录失败
                document.getElementById('passwordError').textContent = data.message || '密码错误';
                document.getElementById('adminPassword').classList.add('error');
                loginBtn.disabled = false;
                loginBtn.querySelector('span:first-child').style.display = 'inline';
                loginBtn.querySelector('.btn-loading').style.display = 'none';
            }
        } catch (error) {
            console.error('登录失败:', error);
            let errorMsg = '网络错误，请稍后重试';
            if (error.message) {
                errorMsg += ' (' + error.message + ')';
            }
            document.getElementById('passwordError').textContent = errorMsg;
            loginBtn.disabled = false;
            loginBtn.querySelector('span:first-child').style.display = 'inline';
            loginBtn.querySelector('.btn-loading').style.display = 'none';
        }
    });
});

// 显示成功消息
function showSuccess(message) {
    let successDiv = document.querySelector('.success-message');
    if (!successDiv) {
        successDiv = document.createElement('div');
        successDiv.className = 'success-message';
        const form = document.querySelector('.login-form');
        form.parentNode.insertBefore(successDiv, form);
    }
    
    successDiv.textContent = message;
    successDiv.classList.add('show');
    
    setTimeout(() => {
        successDiv.classList.remove('show');
    }, 3000);
}

